#处理LOGO用
#清理 empty; 删除文件
#整理格式 ; 重命名文件

require File.expand_path('../../config/environment',  __FILE__)

last_id = 0
while true
  shop_tasks = Shop.all(:conditions => "id > #{last_id} and logo is not null", :order => 'id ASC', :limit => 100)
  break if shop_tasks.blank?
  last_id = shop_tasks.last.id

  shop_tasks.each do |shop_task|
    logo_path = "#{Rails.root}/public#{shop_task.logo}"
    result = %x(file #{logo_path})
    case result
    when /JPEG/
      next
    when /GIF/
      new_logo_path = shop_task.logo[0..-3] + 'gif'
      File.rename logo_path, new_logo_path
      shop_task.logo = new_logo_path
      shop_task.save!
    when /PNG/
      new_logo_path = logo_path[0...-3] + 'png'
      File.rename logo_path, new_logo_path
      shop_task.logo = shop_task.logo[0..-3] + 'png'
      shop_task.save!
    when /empty/, /HTML/
      File.delete logo_path
      shop_task.logo = nil
      shop_task.save!
    else
      Rails.logger.warn "出错,文件类型未知 #{shop_task.id} -- #{result}"
    end
  end
end
